package com.zzyl.nursing.service.impl;

import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zzyl.common.core.domain.model.LoginUser;
import com.zzyl.common.utils.SecurityUtils;
import com.zzyl.nursing.domain.AlertData;
import com.zzyl.nursing.dto.AlertDataPageReqDto;
import com.zzyl.nursing.dto.HandleAlertDataDto;
import com.zzyl.nursing.mapper.AlertDataMapper;
import com.zzyl.nursing.mapper.DeviceDataMapper;
import com.zzyl.nursing.mapper.DeviceMapper;
import com.zzyl.nursing.service.IAlertDataService;
import com.zzyl.nursing.service.IAlertRuleService;
import com.zzyl.nursing.service.IDeviceDataService;
import com.zzyl.system.mapper.SysUserRoleMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;



/**
 * 报警数据Service业务层处理
 * 
 * @author mr.wu
 * @date 2025-05-29
 */
@Service
public class AlertDataServiceImpl extends ServiceImpl<AlertDataMapper, AlertData> implements IAlertDataService
{
    @Autowired
    private AlertDataMapper alertDataMapper;

    /**
     * 查询报警数据
     * 
     * @param id 报警数据主键
     * @return 报警数据
     */
    @Override
    public AlertData selectAlertDataById(Long id)
    {
        return getById(id);
    }

    /**
     * 查询报警数据列表
     * 
     * @param alertData 报警数据
     * @return 报警数据
     */
    @Override
    public List<AlertData> selectAlertDataList(AlertData alertData)
    {
        return alertDataMapper.selectAlertDataList(alertData);
    }

    /**
     * 新增报警数据
     * 
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int insertAlertData(AlertData alertData)
    {
        return save(alertData) ? 1 : 0;
    }

    /**
     * 修改报警数据
     * 
     * @param alertData 报警数据
     * @return 结果
     */
    @Override
    public int updateAlertData(AlertData alertData)
    {
        return updateById(alertData) ? 1 : 0;
    }

    /**
     * 批量删除报警数据
     * 
     * @param ids 需要删除的报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataByIds(Long[] ids)
    {
        return removeByIds(Arrays.asList(ids)) ? 1 : 0;
    }

    /**
     * 删除报警数据信息
     * 
     * @param id 报警数据主键
     * @return 结果
     */
    @Override
    public int deleteAlertDataById(Long id)
    {
        return removeById(id) ? 1 : 0;
    }

    @Autowired
    private DeviceMapper deviceMapper;

    @Autowired
    private SysUserRoleMapper sysUserRoleMapper;
    /**
     * 查询报警数据列表,只有负责该老人的护理员才能看到报警数据
     *
     * @param dto
     * @return
     */
    @Override
    public Page<AlertData> alertDataPageList(AlertDataPageReqDto dto) {

        //获取当前用户ID
        Long userId = SecurityUtils.getUserId();
        Page<AlertData> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        this.lambdaQuery()
                .eq(StringUtils.isNotBlank(dto.getStatus()), AlertData::getStatus, dto.getStatus())
                .eq(StringUtils.isNotBlank(dto.getDeviceName()), AlertData::getDeviceName, dto.getDeviceName())
                .eq(AlertData::getUserId, userId)
                .between(dto.getStartTime() != null && dto.getEndTime() != null, AlertData::getCreateTime, dto.getStartTime(), dto.getEndTime())
                .orderByDesc(AlertData::getCreateTime)
                .page(page);
        return page;
    }

    @Autowired
    private IDeviceDataService deviceDataService;
    @Autowired
    private DeviceDataMapper deviceDataMapper;
    @Autowired
    private IAlertRuleService alertRuleService;
    @Value("${alertRule.roleAdmin}")
    private String roleAdmin;
    @Value("${alertRule.roleRepair}")
    private String roleRepair;
    /**
     * 处理设备报警数据
     *- 如果老人被多个护理员负责，那么多个护理员都可以看到这条数据
     * - 当其中一个护理员处理数据之后，其他负责的护理员也会展示已处理
     * @param dto
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void handleAlertData(HandleAlertDataDto dto) {

        //获取当前登录管理端用户的id
        LoginUser loginUser = SecurityUtils.getLoginUser();//获取登录用户信息
        Long userId = loginUser.getUserId();//获取登录用户ID
        String username = loginUser.getUsername();//获取登录用户名
        //获取该报警数据对相的护理人员id
        AlertData alertData = this.getById(dto.getId());
        List<AlertData> alertDataList = this.lambdaQuery().eq(AlertData::getIotId, alertData.getIotId())
                .eq(AlertData::getNickname, alertData.getNickname())
                .eq(AlertData::getUpdateTime, alertData.getUpdateTime())
                .list();
        List<AlertData> alertDataListList = new ArrayList<>();
        for (AlertData alertData1 : alertDataList) {
            alertData1.setStatus(1);
            alertData1.setProcessingResult(dto.getProcessingResult());
            alertData1.setProcessingTime(dto.getProcessingTime());
            alertData1.setProcessorId(userId);
            alertData1.setProcessorName(username);
            alertDataListList.add(alertData1);
        }
        this.updateBatchById(alertDataListList);
    }
}
